//
//  PickCarColorView.m
//  New_Runway
//
//  Created by mack on 2018/1/31.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "PickCarColorView.h"
@interface PickCarColorView()<UIPickerViewDataSource, UIPickerViewDelegate>
/** 1.选中的字符串 */
@property (nonatomic, strong, nullable)NSString *selectedTitle;
@property (nonatomic, assign)NSInteger colorId;
@property (nonatomic, assign)NSInteger selectedRow;

@end
@implementation PickCarColorView
- (void)initPickView
{
    [super initPickView];
    
    _dataArray=[NSMutableArray mutableCopy];
    _colorArray =[NSMutableArray mutableCopy];
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
    
    
    
    
}

-(void)setDefalutSelectRowStr:(NSString *)defalutSelectRowStr{
    _defalutSelectRowStr=defalutSelectRowStr;
    
    for (NSInteger i = 0; i<_dataArray.count; i++) {
        
        
        if ([_dataArray[i] isEqualToString:_defalutSelectRowStr]) {
            [self.pickerView reloadAllComponents];
            [self.pickerView selectRow:i inComponent:0 animated:NO];
            
        }
    }
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataArray.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 36;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    
    return self.frame.size.width;
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    self.selectedTitle = self.dataArray[row];
    _selectedRow = row;
    [pickerView reloadAllComponents];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
    
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor =[UIColor grayColor];
        }
    }
    
  
    UIView *backview = (UILabel *)view;
    if (!backview)
    {
        
        
        backview = [[UIView alloc] init];
        UIImageView *imageview = [[UIImageView alloc] init];
        
        [backview addSubview:imageview];
        [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backview).mas_equalTo(20);
            make.centerY.equalTo(backview);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(30);
        }];
        NSString *color = self.colorArray[row];
        if ([self.dataArray[row] isEqualToString:@"其他"]) {
            imageview.image = [UIImage imageNamed:@"otherColor"];
        }else if ([self.dataArray[row] isEqualToString:@"双色"]){
            imageview.image = [UIImage imageNamed:@"doubleColor"];
        }else{
            imageview.backgroundColor = [UIColor colorFromHexCode:color];
        }
        imageview.layer.masksToBounds = YES;
        imageview.layer.cornerRadius = 15;
        imageview.layer.borderColor = kColorDarkgray().CGColor;
        imageview.layer.borderWidth = 0.5;
        
        
        UILabel *label = [[UILabel alloc]init];
        label.font=[UIFont systemFontOfSize:16];
        label.textAlignment = 1;
        label.text=self.dataArray[row];
        if (row == _selectedRow) {
            label.textColor = kColorBlack();
        }else{
            label.textColor = kColorLightGray();
        }
        [backview addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backview).mas_equalTo(60);
            make.centerY.equalTo(backview);
            make.right.equalTo(backview);
        }];
        
        
    }
  
    
   

    return backview;
    
    
    
}
#pragma mark -点击确定按钮
- (void)clickConfirmButton
{
    [self.delegate carColorPickView:self selectedColor:self.selectedTitle Colorindex:_selectedRow];
    
    [super clickConfirmButton];
}


- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    _selectedTitle = dataArray.firstObject;
    _selectedRow = 0;
    [self.pickerView reloadAllComponents];
    
}
-(void)setColorArray:(NSMutableArray *)colorArray{
    _colorArray = colorArray;
    [self.pickerView reloadAllComponents];

    
}

@end
